package com.jqr.activity.mycoroutine

import android.annotation.SuppressLint
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import com.jqr.activity.mycoroutine.api.userServiceApi
import com.jqr.activity.mycoroutine.models.Articles
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivity4 : AppCompatActivity() {
    val mainScope= MainScope()
    @OptIn(DelicateCoroutinesApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main1)

        val nameTextView=findViewById<TextView>(R.id.textView);
        nameTextView.text="hello world"

        val  btnClick=findViewById<Button>(R.id.btnClick).also {

            it.setOnClickListener{
              mainScope.launch {
                  val articles= userServiceApi.getAllArticles()
                  nameTextView.text=articles.msg
              }

            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mainScope.cancel()
    }
}

